<template>
	<view class="container">
		<u-form :model="form" ref="uForm" :label-width="220">
			<u-form-item label="商品名称" :required='!form.id||type!=3'>
				<u-input v-if="!form.id||type!=3" placeholder="请输入商品名称" v-model="form.productName" clearable />
				<text v-else>{{form.productName}}</text>
			</u-form-item>
			<u-form-item label="商品标题" :required='!form.id||type!=3'>
				<u-input v-if="!form.id||type!=3" placeholder="请输入商品标题" v-model="form.productTitle" clearable />
				<text v-else>{{form.productTitle}}</text>
			</u-form-item>
			<u-form-item label="商品描述" :required='!form.id||type!=3'>
				<u-input v-if="!form.id||type!=3" placeholder="请输入商品描述" v-model="form.productDesc" border height="140"
					type="textarea" />
				<text v-else>{{form.productDesc}}</text>
			</u-form-item>
			<u-form-item label="商品主图" :class="[!form.id?'formline':'']" :required='!form.id||type!=3'>
				<uploadImg v-if="!form.id||type!=3" :action="action" :header="header" :imageSrc='form.productUrl'
					@getImage="getImage"></uploadImg>
				<u-image v-else :src="form.productUrl" width="200rpx" height="200rpx"></u-image>
			</u-form-item>
			<u-form-item v-if="!form.id||type!=3">
				<view style="color: red;font-size: 24rpx;line-height: 1.4;">*
					图片尺寸规格必须为720*720，图片背景要求非白色，左上角不能放文字/logo，否则审核不通过</view>
			</u-form-item>
			<u-form-item label="商品排序值" :class="[!form.id||type!=3?'formline':'']" :required='!form.id||type!=3'>
				<u-number-box v-if="!form.id||type!=3" :min="1" v-model="form.sortLevel" input-width="160"
					input-height="70"></u-number-box>
				<text v-else>{{form.sortLevel}}</text>
			</u-form-item>
			<u-form-item v-if="!form.id||type!=3">
				<view style="color: red;font-size: 24rpx;line-height: 1.4;">* 排序等级，越大排序越前</view>
			</u-form-item>
			<u-form-item label="商品编码" :required='!form.id||type!=3'>
				<u-input v-if="!form.id||type!=3" placeholder="请输入商品编码" v-model="form.productCode" />
				<text v-else>{{form.productCode}}</text>
			</u-form-item>
			<u-form-item label="订单过期时间" :required='!form.id||type!=3'>
				<u-number-box v-if="!form.id||type!=3" v-model="form.expirationTime" :min="30" input-width="160"
					input-height="70">
				</u-number-box>
				<text v-else>{{form.expirationTime}}</text>
				<text style="margin-left:10rpx;">分钟</text>
			</u-form-item>
			<u-form-item label="商品成本价" :required='!form.id||type!=3'>
				<u-number-box v-if="!form.id||type!=3" placeholder="请输入商品成本价" :min="0" input-width="160"
					input-height="70" v-model="form.productCostPrice" clearable></u-number-box>
				<text v-else>{{form.productCostPrice}}</text>
			</u-form-item>
			<u-form-item label="商品原价" :required='!form.id||type!=3'>
				<u-number-box v-if="!form.id||type!=3" placeholder="请输入商品原价" :min="0" input-width="160"
					input-height="70" v-model="form.productOriginalPrice" clearable></u-number-box>
				<text v-else>{{form.productOriginalPrice}}</text>
			</u-form-item>
			<u-form-item label="商品销售价" :required='!form.id||type!=3'>
				<u-number-box v-if="!form.id||type!=3" placeholder="请输入商品销售价" :min="0" input-width="160"
					input-height="70" v-model="form.productSalePrice" clearable @input="sweetDeductRateChange">
				</u-number-box>
				<text v-else>{{form.productSalePrice}}</text>
			</u-form-item>
			<u-form-item label="糖果抵扣率" v-show="form.productSalePrice">
				<view style="width: 100%;">
					<view style="margin: 20rpx 0;display: flex;align-items: center;" v-if="!form.id||type!=3">
						<view style="flex:1;margin-right: 30rpx;margin-bottom: 30rpx;">
							<u-slider v-model="form.sweetDeductRate" step="1" :max="max"
								@moving="sweetDeductRateChange">
							</u-slider>
						</view>
						<view style="flex:1">
							<u-number-box v-model="form.sweetDeductRate" :max="max" input-width="80" input-height="70"
								@change="sweetDeductRateChange"></u-number-box>
						</view>
					</view>
					<view v-else>{{form.sweetDeductRate}}%</view>
					<view class="cankaojia">
						<text>当天实时参考价格: </text>
						<text style="color: red;">¥{{salePrice}}+ {{saleSweet}}</text>
						<text>个糖果</text>
					</view>
				</view>
			</u-form-item>
			<u-form-item label="限购数量" class="formline">
				<u-number-box v-if="!form.id||type!=3" :min="0" v-model="form.limitNum" input-width="160"
					input-height="70"></u-number-box>
				<text v-else>{{form.limitNum}}</text>
			</u-form-item>
			<u-form-item v-if="!form.id||type!=3">
				<view style="color: red;font-size: 24rpx;line-height: 1.4;">* 0：代表不限购数量</view>
			</u-form-item>
			<u-form-item label="限购次数" class="formline">
				<u-number-box v-if="!form.id||type!=3" :min="0" v-model="form.limitTimes" input-width="160"
					input-height="70"></u-number-box>
				<text v-else>{{form.limitTimes}}</text>
			</u-form-item>
			<u-form-item v-if="!form.id||type!=3">
				<view style="color: red;font-size: 24rpx;line-height: 1.4;">* 0：代表不限购次数</view>
			</u-form-item>
			<u-form-item label="使用规则" class="formline" :required='!form.id||type!=3'>
				<u-input v-if="!form.id||type!=3" placeholder="请输入使用规则" v-model="form.useRules" border height="140"
					type="textarea" />
				<text v-else>{{form.useRules}}</text>
			</u-form-item>
			<u-form-item v-if="!form.id||type!=3">
				<p style="color: red;font-size: 24rpx;line-height: 1.4;">* 例如：周一到周日</p>
			</u-form-item>
			<u-form-item label="是否使用优惠券" :required='!form.id||type!=3'>
				<u-radio-group v-model="form.isCoupon" v-if="!form.id||type!=3">
					<u-radio :name="0">不使用</u-radio>
					<u-radio :name="1">使用</u-radio>
				</u-radio-group>
				<text v-else>{{form.isCoupon==1?'使用':'不使用'}}</text>
			</u-form-item>
			<u-form-item v-if="!form.id||type!=3" label="商品详情图" :required='!form.id||type!=3'>
				<view class="from-right">
					<view style="color: #1F6ECF;" @click="addDetailImage">{{type==1?'添加':'编辑'}}
						<u-icon name="arrow-right"></u-icon>
					</view>
				</view>
			</u-form-item>
			<view v-else>
				<view style="padding:30rpx 0;">商品详情图</view>
				<view v-if="!form.details">暂无详情</view>
				<u-parse v-else="form.details" :html="form.details"></u-parse>
			</view>
		</u-form>
		<view style="margin: 60rpx 0 80rpx 0;" v-if="!form.id||type!=3">
			<u-button type="primary" class="btn" @click="submit()">保存</u-button>
		</view>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import uploadImg from '@/components/uploadImg/index.vue'
	import api from '@/common/uploadApi'
	export default {
		components: {
			uploadImg
		},
		data() {
			return {
				form: {
					id: null, //增传null 编辑传商品id
					mid: '', //商户id 不传
					createUser: '',
					productName: '', //商品名称
					productTitle: '', //商品标题
					productDesc: '', //商品描述
					productUrl: '', //商品主图
					sortLevel: 1, //排序等级，越大排序越前
					productCode: '', //商品编码
					expirationTime: 30, //订单过期时间
					productCostPrice: 0, //商品成本价
					productOriginalPrice: 0, //商品原价
					productSalePrice: 0, //商品销售价
					sweetDeductRate: 0, //糖果抵扣率
					limitNum: 0, //限购数量 默认为0 不限
					limitTimes: 0, //限购次数 默认为0 不限
					useRules: '周一到周日', //使用规则：默认: 周一到周日
					isCoupon: 0, //是否使用优惠券 0 不使用 1 使用
					totalStocks: '', //库存--》可以不传
					warnUnit: '', //预警数量--》可以不传
					details: '', //富文本商品详情
				},
				action: api.foodUploadImage,
				header: {
					"Authorization": 'Bearer ' + uni.getStorageSync('token')
				},
				fileList: [],
				max: 0,
				saleSweet: 0,
				salePrice: 0,
				type: ''
			}
		},

		onLoad(option) {
			this.getDeductRate();
			uni.setNavigationBarTitle({
				title: option.type == 1 ? '添加商品' : option.type == 2 ? '编辑商品' : '查看详情'
			});
			this.type = option.type;
			if (option.id) {
				this.form.id = option.id;
				this.getGoodsDetail(option.id);
			}
		},
		methods: {
			//商户后台获取商品详情
			getGoodsDetail(id) {
				this.$u.offlineApi.foodDetails({
					id: id
				}).then(res => {
					if (res.code == 0) {
						this.fileList = [];
						this.form = {
							id: res.data.id, //增传null 编辑传商品id
							mid: res.data.mid, //商户id 不传
							createUser: res.data.createUser,
							productName: res.data.productName, //商品名称
							productTitle: res.data.productTitle, //商品标题
							productDesc: res.data.productDesc, //商品描述
							productUrl: res.data.productUrl, //商品主图
							sortLevel: res.data.sortLevel, //排序等级，越大排序越前
							productCode: res.data.productCode, //商品编码
							expirationTime: res.data.expirationTime, //订单过期时间
							productCostPrice: res.data.productCostPrice, //商品成本价
							productOriginalPrice: res.data.productOriginalPrice, //商品原价
							productSalePrice: res.data.productSalePrice, //商品销售价
							sweetDeductRate: res.data.sweetDeductRate, //糖果抵扣率
							limitNum: res.data.limitNum, //限购数量 默认为0 不限
							limitTimes: res.data.limitTimes, //限购次数 默认为0 不限
							useRules: res.data.useRules, //使用规则：默认: 周一到周日
							isCoupon: res.data.isCoupon, //是否使用优惠券 0 不使用 1 使用
							totalStocks: '', //库存--》可以不传
							warnUnit: '', //预警数量--》可以不传
							details: res.data.details, //富文本商品详情
						};
						console.log(this.form.details)
						this.sweetDeductRateChange();
					}
				})
			},
			getDeductRate() {
				this.$u.api.deductRate().then(res => {
					if (res.code == 0) {
						this.max = res.data;
					}
				})
			},
			//计算返回商品当天实时参考价格
			sweetDeductRateChange() {
				if (this.form.productSalePrice && this.form.sweetDeductRate >= 0) {
					this.$u.api.calculate({
						productSalePrice: this.form.productSalePrice,
						sweetDeductRate: this.form.sweetDeductRate,
					}).then(res => {
						if (res.code == 0) {
							this.saleSweet = res.data.saleSweet;
							this.salePrice = res.data.salePrice;
						} else {
							this.cankaoPrice = {};
							this.$refs.uToast.show({
								title: res.msg,
								type: 'success',
								icon: true
							});
						}
					})
				}
			},
			//获取主图
			getImage(imageSrc) {
				this.form.productUrl = imageSrc;
			},
			//商品详情图
			addDetailImage() {
				uni.navigateTo({
					url: '/pages/offline/shoping/addDetailImage?id=' + this.form.id
				})
			},
			submit() {
				this.$u.offlineApi.foodEdit(this.form).then(res => {
					if (res.code == 0) {
						uni.showToast({
							icon: 'none',
							title: this.type == 1 ? '添加成功' : '编辑成功'
						});
						uni.switchTab({
							url: '/pages/offline/shoping/index'
						});
					} else {
						uni.showToast({
							icon: 'none',
							title: res.msg
						});
					}
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.container {
		padding: 30rpx;

		.from-right {
			text-align: right;
			width: 100%;
		}
	}

	/deep/ .u-select__header {
		font-size: 32rpx;
	}

	.btn {
		background: #1F6ECF;
		box-shadow: 0px 12rpx 16rpx 0px rgba(28, 100, 224, 0.32);
		border-radius: 43rpx;
	}

	/deep/.formline::after {
		border-width: 0 !important;
	}

	.cankaojia {
		font-size: 26rpx;
	}
</style>
